﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ARS.Control.Custom.DropDownListSelect.Widget
{
    public class DropDownListMultilevel : DropDownListCustom
    {
        public DropDownListMultilevel()
        {
            
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        public string PrentFieldName { get; set; }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            StringBuilder sb=new StringBuilder();
            if (this.DataSource is DataTable)
            {
                Action<DataRow,string> getHtml = (dataRow,str) =>
                {
                    sb.Append(string.Format("<option value=\"{0}\">{2}{1}</option>", dataRow[this.DataValueField], dataRow[this.DataTextField],str));
                };
                //sb.Append("<select>");
                DataTable dataTable = (DataTable) this.DataSource;
                string lastParent=null;
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    string parent = dataRow[this.PrentFieldName].ToString();
                    if (parent != lastParent)
                    {
                        getHtml(dataRow, "");
                        lastParent = parent;
                    }
                    else
                    {
                        getHtml(dataRow, "  ");
                    }
                    
                    
                }
                //sb.Append("</select>");
            }
            writer.Write(sb.ToString());
            base.RenderContents(writer);
        }
    }
}
